package internationalenergytradingplatform.controller.SupplierController;

import internationalenergytradingplatform.VO.PDFDataVO;
import internationalenergytradingplatform.dao.OrderMapper;
import internationalenergytradingplatform.test.PDFDataTest;
import internationalenergytradingplatform.utils.PDFTemplateUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.io.ByteArrayOutputStream;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;


@RestController
@RequestMapping("/export")
@CrossOrigin
@Slf4j
public class ExportPDFController {

    @Autowired
    OrderMapper orderMapper;

    @PostMapping("/export")
    public void exportPdf(HttpServletResponse response, Integer orderId) throws Exception{
        //通过订单编号查询订单相关信息
        PDFDataVO invoiceInfo = orderMapper.getInvoiceInfo(orderId);
        ByteArrayOutputStream baos = null;
        OutputStream out = null;
        try {
            // 模板中的数据，实际运用从数据库中查询
            Map<String,Object> data = new HashMap<>();
            data.put("title", "国际能源通发票");
            data.put("invoiceNumber", "0000054250");
            data.put("date", "2024-04-08");
            data.put("GYSXinXi", "供应商信息");
            data.put("GYSMingCheng", "公司名称");
            data.put("supplierName", invoiceInfo.getSupplierName());
            data.put("GYSDiZhi", "公司地址");
            data.put("supplierAddress", invoiceInfo.getSupplierAddress());
            data.put("GYSLianXiFangShi", "联系方式");
            data.put("supplierPhoneNumber", invoiceInfo.getBuyerContact());
            data.put("CGSXinXi", "采购商信息");
            data.put("CGSMingCheng", "公司名称");
            data.put("buyerName", invoiceInfo.getBuyerName());
            data.put("CGSDiZhi", "公司地址");
            data.put("buyerAddress", invoiceInfo.getBuyerAddress());
            data.put("CGSLianXiFangShi", "联系方式");
            data.put("buyerPhoneNumber", invoiceInfo.getBuyerContact());
            data.put("ShangPinMingCheng", "商品名称");
            data.put("ShuLiang", "商品数量");
            data.put("DanJia", "商品单价");
            data.put("ZongJi", "总计");
            data.put("productName", invoiceInfo.getCommodityName());
            data.put("productNumber", invoiceInfo.getCommodityAmount() + invoiceInfo.getUnitInventory());
            data.put("productPrice", invoiceInfo.getCommodityPrice() + invoiceInfo.getUnitPrice());
            data.put("totalPriceGoods", invoiceInfo.getCommodityAmount() + "*" + invoiceInfo.getCommodityPrice() + invoiceInfo.getUnitPrice());
            data.put("Thanks", "感谢您的支持！求您啦！");

            baos = PDFTemplateUtil.createPDF(data, "pdf测试模板.ftl");;
            // 设置响应消息头，告诉浏览器当前响应是一个下载文件
            response.setContentType( "application/x-msdownload");
            // 告诉浏览器，当前响应数据要求用户干预保存到文件中，以及文件名是什么 如果文件名有中文，必须URL编码
            String fileName = URLEncoder.encode("testFiles.pdf", "UTF-8");
            response.setHeader( "Content-Disposition", "attachment;filename=" + fileName);
            out = response.getOutputStream();
            baos.writeTo(out);
            baos.close();
        } catch (Exception e) {
            e.printStackTrace();
            throw new Exception("导出失败：" + e.getMessage());
        } finally{
            if(baos != null){
                baos.close();
            }
            if(out != null){
                out.close();
            }
        }
    }
}
